package org.hefan.user.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.hefan.basic.exception.BusinessException;
import org.hefan.basic.util.AjaxResult;
import org.hefan.user.dto.LoginDto;
import org.hefan.user.dto.PhoneLoginDto;
import org.hefan.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 账号密码登录
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto) {
        try {
            return logininfoService.accountLogin(loginDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("登陆错误," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }

    /**
     * 微信用户扫码登陆
     * @param map
     * @return
     */
    @PostMapping("/wxUser")
    public AjaxResult wxUserLogin(@RequestBody Map<String, String> map) {
        try {
            return logininfoService.wxUserLogin(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("登陆错误," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }

    /**
     * 微信用户第一次扫码登陆需要绑定手机号
     * @param map
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult wxUserBinder(@RequestBody Map<String, String> map) {
        try {
            return logininfoService.wxUserBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("登陆错误," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }

    /**
     * 手机验证码登陆
     * @param
     * @return
     */
    @PostMapping("/phone")
    public AjaxResult phoneLogin(@RequestBody PhoneLoginDto phoneLoginDto, HttpServletRequest request) {
        try {
            System.out.println(phoneLoginDto);
            return logininfoService.phoneLogin(phoneLoginDto, request);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("登陆错误," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }
}
